
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:请求发送找回密码的邮箱验证码
 *
 * @author wcj
 * @since 2015年8月18日 下午4:56:26
 */
public class RequestEmailValidateCodeTask {

    public ResponseMessage request(String username, String email) throws AppException {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        ResponseMessage respObj = new ResponseMessage();
        ResponsePackage resp = new RequestEmailVerifiyCodeResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("cmd", ProtocolCmd.CMD_SEND_VALIDATE_CODE_EMAIL_FOR_RESET_PSD);
        params.put("username", username);
        params.put("businessid", 3);
        params.put("email", email);
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (AppException e) {
            throw e;
        }
    }

    static class RequestEmailVerifiyCodeResponsePackage extends
            HttpBaseResponsePackage<ResponseMessage> {

        @Override
        protected void handleResponse(ResponseMessage response, String jsonStr) {
            try {
                LogUtil.d("response", "RequestEmailValidateCodeTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {

                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (response.isUseful()
                            && ProtocolCmd.CMD_SEND_VALIDATE_CODE_EMAIL_FOR_RESET_PSD.equals(cmd)) {
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
            }
        }
    }
}
